# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Renato Silva <br.renatosilva@gmail.com>

_realname=harfbuzz
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.7.5
pkgrel=2
pkgdesc="OpenType text shaping engine (mingw-w64)"
arch=('any')
url="https://www.freedesktop.org/wiki/Software/HarfBuzz"
license=("MIT")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-icu"
             "${MINGW_PACKAGE_PREFIX}-gobject-introspection"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "${MINGW_PACKAGE_PREFIX}-cairo")
depends=("${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-glib2"
         "${MINGW_PACKAGE_PREFIX}-graphite2")
options=('strip' 'staticlibs')
optdepends=(
  "${MINGW_PACKAGE_PREFIX}-icu: harfbuzz-icu support"
  "${MINGW_PACKAGE_PREFIX}-cairo: hb-view program"
)
source=("https://www.freedesktop.org/software/harfbuzz/release/${_realname}-${pkgver}.tar.bz2")
sha256sums=('84574e1b1f65ca694cb8fb6905309665c0368af18a312357f8ff886ee2f29563')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  #autoreconf -fi
}

build() {
  # Build static and shared libs separately due to necessity of defining DGRAPHITE2_STATIC
  # manually when building static version
  export _static_dir="${srcdir}/build-${MINGW_CHOST}-static"
  export _shared_dir="${srcdir}/build-${MINGW_CHOST}-shared"

  # static build
  [[ -d "${_static_dir}" ]] && rm -rf "${_static_dir}"
  mkdir -p "${_static_dir}" && pushd "${_static_dir}"
  CFLAGS=-DGRAPHITE2_STATIC CXXFLAGS=-DGRAPHITE2_STATIC ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --enable-shared=no \
    --enable-static=yes \
    --enable-introspection \
    --with-graphite2=yes \
    --with-gobject
  make
  popd

  # shared build
  [[ -d "${_shared_dir}" ]] && rm -rf "${_shared_dir}"
  mkdir -p "${_shared_dir}" && pushd "${_shared_dir}"
  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --enable-shared=yes \
    --enable-static=no \
    --enable-introspection \
    --with-graphite2=yes \
    --with-gobject
  make
  popd
}

check(){
  export _static_dir="${srcdir}/build-${MINGW_CHOST}-static"
  export _shared_dir="${srcdir}/build-${MINGW_CHOST}-shared"

  # static build 
  pushd "${_static_dir}"
  make check
  popd

  # shared build
  pushd "${_shared_dir}"
  make check
  popd
}

package() {
  export _static_dir="${srcdir}/build-${MINGW_CHOST}-static"
  export _shared_dir="${srcdir}/build-${MINGW_CHOST}-shared"

  # static build
  pushd "${_static_dir}"
  make DESTDIR="${pkgdir}" install
  popd

  # shared build
  pushd "${_shared_dir}"
  make DESTDIR="${pkgdir}" install
  popd

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
}
